package com.weitu.securitycheck.service.impl;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.securitycheck.entity.CheckInfo;
import com.weitu.securitycheck.entity.CheckInfoCriteria;
import com.weitu.securitycheck.mapper.CheckInfoMapper;
import com.weitu.securitycheck.service.CheckInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckInfoServiceImpl implements CheckInfoService {
    private final CheckInfoMapper checkInfoMapper;

    private static final Logger logger = LoggerFactory.getLogger(CheckInfoServiceImpl.class);

    @Autowired
    public CheckInfoServiceImpl(CheckInfoMapper checkInfoMapper) {
        this.checkInfoMapper = checkInfoMapper;
    }

    @Override
    public int countByCriteria(CheckInfoCriteria example) {
        int count = this.checkInfoMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public CheckInfo selectByPrimaryKey(String id) {
        return this.checkInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckInfo> selectByCriteria(CheckInfoCriteria example) {
        return this.checkInfoMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.checkInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(CheckInfo record) {
        return this.checkInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(CheckInfo record, CheckInfoCriteria example) {
        return this.checkInfoMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(CheckInfo record) {
        return this.checkInfoMapper.insertSelective(record);
    }

    @Override
    @Transactional
    public boolean saveAccessoryAndInsert(CheckInfo checkInfo) {
        if (AccessoryUtil.saveAccerssoryInfo(checkInfo.getId())) {
            if (checkInfoMapper.insertSelective(checkInfo) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional
    public boolean saveAccessoryAndUpdate(CheckInfo checkInfo) {
        if (AccessoryUtil.saveAccerssoryInfo(checkInfo.getId())) {
            if (checkInfoMapper.updateByPrimaryKeySelective(checkInfo) > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<String> selectCheckPlanIds(String id) {
        return this.checkInfoMapper.selectCheckPlanIds(id);
    }
}